package com.right.health.app.ui.medical;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.dialog.DateBottomPickerDialog;
import com.right.health.app.net.SickHistoryApi;

import static com.right.health.app.constants.BroadCastCode.REFRESH_SURGERY;

/*
 * Created by hopeliao on 2019-09-30
 * 添加手术史
 *
 */
public class SurgeryAddActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private EditText mNameEt;

    private TextView mChooseTimeTv;

    private EditText mDescEt;

    private DateBottomPickerDialog mDateDialog;

    private SickHistoryApi mSickHistoryApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.surgery_add);
    }

    @Override
    public void findView() {
        mNameEt = (EditText) findViewById(R.id.et_name);
        mChooseTimeTv = (TextView) findViewById(R.id.tv_choose_time);
        mChooseTimeTv.setOnClickListener(this);
        mDescEt = (EditText) findViewById(R.id.et_desc);

        findViewById(R.id.tv_save).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mSickHistoryApi = new SickHistoryApi();
        mSickHistoryApi.setListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_save) {
            send2Net();
        } else if (v.getId() == R.id.tv_choose_time) {
            getDateDialog().show();
        }
    }

    private void send2Net() {
        String name = mNameEt.getText().toString().trim();
        if(TextUtils.isEmpty(name)) {
            ToastUtil.show(this, mNameEt.getHint().toString());
            return;
        }
        String time = mChooseTimeTv.getText().toString().trim();
        if(TextUtils.isEmpty(time)) {
            ToastUtil.show(this, mChooseTimeTv.getHint().toString());
            return;
        }
        String desc = mDescEt.getText().toString().trim();
        if(TextUtils.isEmpty(desc)) {
            ToastUtil.show(this, mDescEt.getHint().toString());
            return;
        }

        mSickHistoryApi.addSurgery(App.getInstance().getCurrentMember().id, name, time, desc);
    }

    private void dismissDateDialog() {
        if (mDateDialog != null && mDateDialog.isShowing()) {
            mDateDialog.dismiss();
        }
    }

    private DateBottomPickerDialog getDateDialog() {
        if (mDateDialog == null) {
            mDateDialog = new DateBottomPickerDialog(this);
            mDateDialog.setDelegate((tag1, year, month, date) -> {
                dismissDateDialog();

                mChooseTimeTv.setText(year + "-" + month + "-" + date);
            });
        }
        return mDateDialog;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissDateDialog();
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            ToastUtil.show(this, "添加成功");
            sendBroadcast(REFRESH_SURGERY);
            finish();
        }
    }
}
